package com.xiaochen.baidu.service;

import com.xiaochen.baidu.common.design.link.BusinessLinkedList;
import com.xiaochen.baidu.common.design.strategy.AbstractMultiThreadStrategyRouter;
import com.xiaochen.baidu.common.design.strategy.StrategyHandler;
import com.xiaochen.baidu.model.entity.RequestEntity;
import com.xiaochen.baidu.model.entity.RuleCommandEntity;
import com.xiaochen.baidu.service.factory.DefaultRuleFilterFactory;
import com.xiaochen.baidu.service.factory.DefaultStrategyFactory;
import com.xiaochen.baidu.service.node.LoginNode;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xc
 * @date 2025/10/11/23:25
 * @desc
 */
@Service
public class BaiduService implements IBaiduService {

    @Resource
    private IRedisService redisService;

    @Resource
    private BusinessLinkedList<RuleCommandEntity, DefaultRuleFilterFactory.RuleDynamicContext, AbstractMultiThreadStrategyRouter> ruleFilter;

    @Override
    public void save(String username, String password) {
        //保存1个月
        redisService.setValue("baidu:info", username + ":" + password, 1000L * 60 * 60 * 24 * 30);
    }

    @Override
    public String parse(String url, String num) throws Exception {
        //解析url（默认是有surl和pwd的）
        String surl = null, pwd = null, dir = null;
        Pattern combinedPattern = Pattern.compile("s/([^?#]+)[?]pwd=([^&#]+)");
        Matcher combinedMatcher = combinedPattern.matcher(url);
        if (combinedMatcher.find()) {
            surl = combinedMatcher.group(1);
            pwd = combinedMatcher.group(2);
        }
        //缓存查找
        String surlInfo = (String) redisService.getValue("baidu:" + surl);
        if (surlInfo != null) {
            String[] split = surlInfo.split(":");
            pwd = split[0];
            dir = split[1];
        }else {
            dir = "/";
        }
        String kdownSession = (String) redisService.getValue("baidu:kdownSession");
        //创建RuleCommandEntity
        RuleCommandEntity ruleCommandEntity = RuleCommandEntity.builder()
                .kdownSession(kdownSession)
                .surl(surl)
                .pwd(pwd)
                .dir(dir)
                .build();
        StrategyHandler strategyHandler = ruleFilter.handle(ruleCommandEntity, DefaultRuleFilterFactory.RuleDynamicContext.builder().build());
        RequestEntity requestEntity = RequestEntity.builder().build();
        if (strategyHandler instanceof LoginNode){
            String value = (String) redisService.getValue("baidu:info");
            String[] split = value.split(":");
            requestEntity.setUsername(split[0]);
            requestEntity.setPassword(split[1]);
        }
        String result = (String) strategyHandler.handle(requestEntity, DefaultStrategyFactory.StrategyDynamicContext.builder()
                .kdownSession(kdownSession)
                .surl(surl)
                .pwd(pwd)
                .dir(dir)
                .num(num)
                .build());
        return result;
    }
}
